package com.ruoyi.web.plan.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.web.plan.domain.TaskInfo;
import com.ruoyi.web.plan.service.ITaskInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.web.plan.domain.TaskModule;
import com.ruoyi.web.plan.service.ITaskModuleService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.utils.poi.ExcelUtil;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 项目模块Controller
 *
 * @author ruoyi
 * @date 2024-12-25
 */
@RestController
@RequestMapping("/plan/module")
public class TaskModuleController extends BaseController {
    @Autowired
    private ITaskModuleService taskModuleService;
    @Autowired
    private ITaskInfoService taskInfoService;

    /**
     * 查询项目模块列表
     */
    @SaCheckPermission("plan:module:list")
    @GetMapping("/list")
    public TableDataInfo list(TaskModule taskModule) {
        startPage();
        List<TaskModule> list = taskModuleService.selectTaskModuleList(taskModule);
        return getDataTable(list);
    }

    /**
     * 导出项目模块列表
     */
    @SaCheckPermission("plan:module:export")
    @Log(title = "项目模块", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TaskModule taskModule) {
        List<TaskModule> list = taskModuleService.selectTaskModuleList(taskModule);
        ExcelUtil<TaskModule> util = new ExcelUtil<TaskModule>(TaskModule.class);
        util.exportExcel(response, list, "项目模块数据");
    }

    /**
     * 获取项目模块详细信息
     */
    @SaCheckPermission("plan:module:query")
    @GetMapping(value = "/{moduleId}")
    public AjaxResult getInfo(@PathVariable("moduleId") Long moduleId) {
        return success(taskModuleService.selectTaskModuleByModuleId(moduleId));
    }

    /**
     * 新增项目模块
     */
    @SaCheckPermission("plan:module:add")
    @Log(title = "项目模块", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TaskModule taskModule) {
        int result = taskModuleService.insertTaskModule(taskModule);
        return toAjax(result);
    }

    /**
     * 修改项目模块
     */
    @SaCheckPermission("plan:module:edit")
    @Log(title = "项目模块", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TaskModule taskModule) {
        return toAjax(taskModuleService.updateTaskModule(taskModule));
    }

    /**
     * 删除项目模块
     */
    @SaCheckPermission("plan:module:remove")
    @Log(title = "项目模块", businessType = BusinessType.DELETE)
    @DeleteMapping("/{moduleIds}")
    public AjaxResult remove(@PathVariable Long[] moduleIds) {
        if (moduleIds == null || moduleIds[0] == null) {
            return AjaxResult.error("参数不能为空");
        }
        TaskInfo query = new TaskInfo();
        query.setModuleId(moduleIds[0]);
        List<TaskInfo> taskInfoList = taskInfoService.selectTaskInfoList(query);
        if (taskInfoList != null && taskInfoList.size() > 0) {
            return AjaxResult.error("请先删除模块下的任务");
        }
        return toAjax(taskModuleService.deleteTaskModuleByModuleIds(moduleIds));
    }
}
